import controller.Controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import model.CSVparser;
import model.DataTable;
import model.FileParser;
import model.ModelException;
import model.XMLparser;

/**
 * 
 * @author tyy
 * modified slightly by Michael Elgart
 *
 */
public class Main {
    private static final String TITLE = "GEDIVA";
    private static final String DEFAULT_LANGUAGE = "English";
    private static JFileChooser ourChooser;

    /**
     * empty constructor
     */
    protected Main() { }
    /**
     * 
     * @param args 
     */
    public static void main (String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ourChooser = new JFileChooser(System.getProperties().getProperty("user.dir"));
        int response = ourChooser.showOpenDialog(frame);
        if (response == JFileChooser.APPROVE_OPTION) {
            File file = ourChooser.getSelectedFile();
            try {
                InputStream stream = new FileInputStream(file);
                DataTable data = new DataTable(file.getName());
                FileParser parser = null;
                if (file.getName().endsWith(".csv")) {
                    parser = new CSVparser();
                }
                else if (file.getName().endsWith(".xml")) {
                    parser = new XMLparser();
                }
                else {
                    throw new ModelException("File type not supported");
                }
                parser.readInput(stream, data);
                Controller c = new Controller(frame, TITLE, DEFAULT_LANGUAGE, data);
                frame.add(c.getVisualization());
                frame.pack();
                frame.setVisible(true);
            }
            catch (FileNotFoundException e) {
                throw new ModelException("File not found", e);
            }
        }
    }
}
